package de.unidue.langtech.teaching.pp.example;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;

import de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.tweet.EMO;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import ude.DetectedSentiment;

/**
 * The baseline sets detected sentiment regarding only the emoticons.
 * 
 * @author hanalie
 *
 */
public class BaselineEmoticons
    extends JCasAnnotator_ImplBase
{

    @Override
    public void process(JCas jcas)
        throws AnalysisEngineProcessException
    {
        System.out.println("Document is: " + jcas.getDocumentText());
        
        Collection<EMO> emos = JCasUtil.select(jcas, EMO.class);
        for (EMO emo : emos) {
        	System.out.println(emo.getCoveredText());
        	System.out.println(emo);
        }
        System.out.println("CAS contains " + emos.size() + " emos.");   
        
        String output ="";
		try {
			output = getSentiment(jcas);
		} catch (AnalysisEngineProcessException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        DetectedSentiment sentimentAnno = new DetectedSentiment(jcas);
        sentimentAnno.setSentiment(output);
        sentimentAnno.addToIndexes();
  
    }
    
    public String getSentiment(JCas jcas) throws AnalysisEngineProcessException, IOException {
        // positive/negative word lists are imported
		File positiveFile = new File("src/test/resources/test/positive-words-emoticons.txt");
		File negativeFile = new File("src/test/resources/test/negative-words-emoticons.txt");
		
		
		
		try {
		//read all lines of the word lists separately; Quelle: http://comments.gmane.org/gmane.comp.ai.gate.general/8386
		List<String> positiveLines = FileUtils.readLines(positiveFile);	
		List<String> negativeLines = FileUtils.readLines(negativeFile);	
		
		HashSet<String> positiveWords = new HashSet<String>();
		positiveWords.addAll(positiveLines);
		HashSet<String> negativeWords = new HashSet<String>();
		negativeWords.addAll(negativeLines);
		
		// saves number of negative/positive tokens in one document
		int posCounter=0;
		int negCounter=0;
		
		
		 Collection<EMO> emos = JCasUtil.select(jcas, EMO.class); 
		 for (EMO emoAnno: emos) {
			 String emo = emoAnno.getCoveredText();
	        
	        if (positiveWords.contains(emo)){
	        	posCounter++;
	        }
	        
	        if (negativeWords.contains(emo)){
				negCounter++;
	        }
		 }   
	
	    System.out.println("neg:" + negCounter);
	    System.out.println("pos:" + posCounter);
		
	    // score is used to evaluate the sentiment of each document 
	    // to mask the quotes so that a string contains a quotation mark; source: http://www.unixboard.de/vb3/showthread.php?12401-Anf%FChrungszeichen-%28-quot-%29-in-einen-String
		int score = (negCounter - posCounter);
		
		String output;
		
		if (score < 0) {
			output = "\"positive\"";					
		} else if (score > 0)  {
			output = "\"negative\"";
	       
		} else {
			output = "\"neutral\"";
		}
		return output;
		}	

	catch(Exception e)
	{
		e.printStackTrace();
	}
		return null;
    }

    }

		
	